package com.clh.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.clh.common.PageInfo;
import com.clh.dto.Result;
import com.clh.entity.Car;
import com.clh.entity.Owner;
import com.clh.service.CarService;
import com.clh.service.impl.CarServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class CarServlet
 */
@WebServlet("/CarServlet")
public class CarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	private CarService carService = new CarServiceImpl();

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		
		if(op == null || "".equals(op)) { //分页查询条件
			this.doCarPageQuery(request,response);
		} else if("delete" .equals(op)){ //车辆通过ID删除
			this.doDeleteById(request,response);
		} else if("page".equals(op)){ //执行分页查询
			this.doCarPageQuery(request,response);
		} else if("add".equals(op)){ //执行车辆的添加
			this.doAddCars(request,response);
		} else if("findById".equals(op)){ //执行单个查询
			this.doFindById(request,response);
		} else if("update".equals(op)){ //执行更新（修改）
			this.doUpdate(request,response);
		} else if("findByPhone".equals(op)){ //执行单通过电话查询车辆信息
			this.doFindByPhone(request,response);
		} 
		
	}
	/**
	 * 执行单通过电话查询车辆信息
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void doFindByPhone(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Result result = new Result();
		String carPhone = request.getParameter("carPhone");
		System.out.println(carPhone);
		if(carPhone == null || "".equals(carPhone)) {
			result.fail(0, "参数错误");
		}else {
			//调用业务层
			List<Car> car = carService.getCarByPhone(carPhone);
			
			if(car != null) {
				result.ok(car);
			}else {
				result.fail(0, "没有相关的数据");
			}
		}
		// 设置响应的内容格式
		response.setContentType("application/json;charset=utf-8");
		// 利用第三方的JASON转换
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		// 创建响应并将数据返回给浏览器
		response.getWriter().write(json);
		
	}
	/**
	 * (修改)更新车辆信息方法
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void doUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Result result = new Result();
		String carId = request.getParameter("carId");
		String carUserName = request.getParameter("carUserName");
		String carCard = request.getParameter("carCard");
		String carPhone = request.getParameter("carPhone");
		String roomNo = request.getParameter("roomNo");
		
		Car car = new Car();
		car.setCarId(carId);
		car.setCarUserName(carUserName);
		car.setCarCard(carCard);
		car.setCarPhone(carPhone);
		Owner owner = new Owner();
		owner.setRoomNo(roomNo);
		car.setOwner(owner);
		
		boolean f = carService.modifyCar(car);
		if(f) {
			result.ok(f);
		}else {
			result.fail(0, "商品修改失败");
		}
		// 设置响应的内容格式
		response.setContentType("application/json;charset=utf-8");
		// 利用第三方的JASON转换
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		// 创建响应并将数据返回给浏览器
		response.getWriter().write(json);
		
	}
	/**
	 * 执行单个查询的方法
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void doFindById(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Result result = new Result();
		String carId = request.getParameter("carId");
		if(carId == null || "".equals(carId)) {
			result.fail(0, "参数错误");
		}else {
			//调用业务层
			Car car = carService.getCarById(carId);
			if(car != null) {
				result.ok(car);
			}else {
				result.fail(0, "没有相关的数据");
			}
		}
		// 设置响应的内容格式
		response.setContentType("application/json;charset=utf-8");
		// 利用第三方的JASON转换
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		// 创建响应并将数据返回给浏览器
		response.getWriter().write(json);
	}
	/**
	 * 执行车辆添加的方法
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void doAddCars(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//返回统一的结果对象
		Result result = new Result();
		//接收请求参数
		String carUserName = request.getParameter("carUserName");
		String carCard = request.getParameter("carCard");
		String carPhone = request.getParameter("carPhone");
		String roomNo = request.getParameter("roomNo");
		
		//构建商品对象
		Car car = new Car();
		car.setCarUserName(carUserName);
		car.setCarCard(carCard);
		car.setCarPhone(carPhone);
		Owner owner = new Owner();
		owner.setRoomNo(roomNo);
		car.setOwner(owner);
		
		boolean f = carService.addCar(car);
		if(f) {
			result.ok(f);
		}else {
			result.fail(0, "添加商品失败");
		}
		
		// 设置响应的内容格式
		response.setContentType("application/json;charset=utf-8");
		// 利用第三方的JASON转换
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		// 创建响应并将数据返回给浏览器
		response.getWriter().write(json);
		
		
	}
	/**
	 * 车辆ID删除方法
	 * @param request
	 * @param response
	 */
	private void doDeleteById(HttpServletRequest request, HttpServletResponse response) {
		Result result = new Result();
		try {
			String carId = request.getParameter("carId");
			if(carId == null || "".equals(carId)) {
				result.fail(0, "参数错误");
			}else {
				int n = carService.deleteById(carId);
				if(n > 0) {
					result.ok(true).setMessage("删除成功");
				}else {
					result.fail(0, "删除失败，可能是已经被删除数据不存在");
				}
			}
			// 设置响应的内容格式
			response.setContentType("application/json;charset=utf-8");
			// 利用第三方的JASON转换
			ObjectMapper objectMapper = new ObjectMapper();
			String json = objectMapper.writeValueAsString(result);
			// 创建响应并将数据返回给浏览器
			response.getWriter().write(json);
			
		} catch (Exception e) {
			//响应错误提示
			result.fail(0, "服务器出错");
			e.printStackTrace();
		}
	}

	private void doCarPageQuery(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Result result = new Result();
		//接收请求参数
		String carId = request.getParameter("carId");
		String carUserName = request.getParameter("carUserName");
		String carCard = request.getParameter("carCard");
		String carPhone = request.getParameter("carPhone");
		String roomNo = request.getParameter("roomNo");
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		
		
		if ((pageNum == null || "".equals(pageNum) || Integer.parseInt(pageNum) < 0)
				|| (pageSize == null || "".equals(pageSize) || Integer.parseInt(pageSize) < 0)) {
			result.fail(0, "参数错误");
		} else {
			
			Car car = new Car();
			car.setCarUserName(carUserName);
			car.setCarId(carId);
			car.setCarCard(carCard);
			car.setCarPhone(carPhone);
			PageInfo pageInfo = carService.getCarListByPage(car, Integer.parseInt(pageNum), Integer.parseInt(pageSize));
			if (!pageInfo.getList().isEmpty()) {
				result.ok(pageInfo);

			} else {
				result.fail(0, "没有相关数据");
			}
		}
		// 设置响应的内容格式
		response.setContentType("application/json;charset=utf-8");
		// 利用第三方的JASON转换
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		// 创建响应并将数据返回给浏览器
		response.getWriter().write(json);
	}
	
}
